package vn.com.mybudget;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import vn.com.mybudget.base.BaseRouter;
import vn.com.mybudget.base.HTMLView;
import vn.com.mybudget.base.PartialView;
import vn.com.mybudget.base.ViewBase;
import vn.com.mybudget.base.XMLView;
import vn.com.mybudget.bo.TransactionBO;
import vn.com.mybudget.entities.Entries;
import vn.com.mybudget.entities.Entry;

@SuppressWarnings("serial")
public class TransactionServlet extends BaseRouter {

	public ViewBase AddTransaction() {
		return new PartialView("addtransaction",this);
	}
	
	public ViewBase UpdateTransaction() {
		return new PartialView("updatetransaction",this);
	}
	
	public boolean AddTransactionSubmit(Entry transaction) throws IOException {
		if (transaction == null){
			return false;
		}
		TransactionBO transactionBO = new TransactionBO();
		return transactionBO.AddTransaction(transaction,_currentAccount);
	}
	
	public boolean UpdateTransactionSubmit(Entry transaction){
		TransactionBO tBo = new TransactionBO();
		boolean rs = tBo.UpdateTransaction(transaction,_currentAccount);
		return rs;
	}
	
	public ViewBase GetTransactionByDate(String viewDate) throws ParseException {
		TransactionBO tBo = new TransactionBO();
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Date dateStr = formatter.parse(viewDate);
		List<Entry> list = tBo.GetTransactionByDate(dateStr,_currentAccount);
		Entries tss = new Entries();
		for(int i = 0 ; i< list.size(); i++){
			tss.getEntries().add(list.get(i));
		}
		return new XMLView(tss);
	}
	
	public ViewBase GetTransactionByMonth(String viewDate) throws ParseException {
		TransactionBO tBo = new TransactionBO();
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
        Date dateStr = formatter.parse(viewDate);
		List<Entry> list = tBo.GetTransactionByMonth(dateStr,_currentAccount);
		Entries tss = new Entries();
		for(int i = 0 ; i< list.size(); i++){
			tss.getEntries().add(list.get(i));
		}
		return new XMLView(tss);
	}
	
	public String GetCurrentBalance(){
		TransactionBO tBo = new TransactionBO();
		long balance = tBo.GetCurrentBalance(this._currentAccount);
		return balance+"";
	}
	
	public boolean DeleteTransactionByKey(String key){
		TransactionBO tBo = new TransactionBO();
		boolean rs = tBo.DeleteTransactionByKey(key,_currentAccount);
		return rs;
	}
	
	
}
